<?php
namespace Admin\Controller;
use Think\Controller;
class PrizeController extends Controller
{
    public function index()
    {
        $this->display();
       
    }

    /*奖品列表*/
    public function prizelist(){
        $prize = M('prize');
        $prizelist = $prize->order("prizeid asc")->field('prizeid,prizename,prizepic,prizeintro,prizepoint')->select();

        for($index=0;$index<count($prizelist);$index++)
        {
            $prizeist[$index]['number']=$index+1;
        }

        $this->assign('prizelist',$prizelist);
        $this->display();
    }



// 删除奖品
     public function delprize()
    {
        $prizeid=I('prizeid');
        $prizesModel = M('prize');
        $prize=$prizesModel->where("prizeid=$prizeid")->find();
        // unlink(realpath("./Public/Uploads/logo/".$user["photo"]));
        if($prizesModel->delete($prizeid)) {
            $this->success('删除成功');
        }
        else {
            $this->error('删除失败');
        }
           
    }





    //添加奖品
    public function addprize(){
        $this->display();
    }  
        public function newprize(){

 $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']);
        
        //没有头像
        $prizeModel = M('prize'); // 实例化User模型
        $createresult=$prizeModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
            $prizeModel->createtime =  date("Y-m-d H:i:s"); // 设置用户的创建时间
             if ($file['prizepic']['size']==0) {//没有上传轮播图片
                        $prizeModel->prizepic= "nopic.jpg"; // 用户不上传头像默认nopic.jpg
                    }else{//有图片
                            $info=$this->Upload();
                            $prizeModel->prizepic = $info['prizepic']['savename']; // 保存上传的照片根据需要自行组装
                          }
                 $addresult=$prizeModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('上传成功！', '../Prize/prizelist');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($brandsModel->getError());  
              }
 
    }
    public function Upload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Uploads/photo/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                return $info;
                         }
    } 




//修改
public function editprize(){
        $prize=M('prize');
        $prizeid=$_GET['prizeid'];
        $res = $prize->where("prizeid=$prizeid")->select();      
        $this->prizelist=$res;//name属性对应的值 $this->对应值=$find;
        $this->display();
    }
public function update(){
           $data['prizename']=$_POST['prizename'];
           $data['prizeintro']=$_POST['prizeintro'];
           $data['prizepoint']=$_POST['prizepoint'];

           $prizeid = $_POST['prizeid'];

           $prizeModel = M('prize'); // 实例化User模型
             $data['createtime'] =  date("Y-m-d H:i:s"); // 设置用户的创建时间
             // print_r($_FILES);
             if ($_FILES['prizepic']['size']>0) {//没有上传轮播图片
                            $oldname = $prizeModel->field('prizepic')->where('prizeid ='.$prizeid)->find();
                            unlink('./Public/Uploads/photo/'.$oldname['prizepic']);

                          $filename = explode('.', $_FILES['prizepic']['name']);
                          $extendsname = end($filename);
                        //  print_r()
                          $savename = rand(0,99).time().'.'.$extendsname;
                          move_uploaded_file($_FILES['prizepic']['tmp_name'], './Public/Uploads/photo/'.$savename);
                          $data['prizepic'] = $savename;
                         
                  }
               //  $addresult=$trainModel->add(); // 写入用户数据到数据库
                  
                     if (false != $prizeModel->where('prizeid ='.$prizeid)->save($data))
                             {
                            $this->success('修改成功！', '../Prize/prizelist');
                             }else{ 
                                     $this->error('修改错误'); 
                                   }
                                    } 




}